<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Go语言数据类型 | anyang的博客</title>
    <meta name="description" content="千里之行,始于足下">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="icon" href="/img/favicon.ico">
  <script charset="utf-8" src="/js/main.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
  <meta name="keywords" content="福小林,奔奔,ourLang,CentOS,JAVA,vue组件,lsdCloud">
  <script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-146891701-1"></script>
  <script>window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-146891701-1');</script>
  <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?56eae8eec590ccaef1d5ff99d766f315";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    
    <link rel="preload" href="/assets/css/0.styles.9fff9873.css" as="style"><link rel="preload" href="/assets/js/app.1bc80adb.js" as="script"><link rel="preload" href="/assets/js/2.7c0608ab.js" as="script"><link rel="preload" href="/assets/js/66.cd973084.js" as="script"><link rel="prefetch" href="/assets/js/10.81caa2ab.js"><link rel="prefetch" href="/assets/js/100.05bb269d.js"><link rel="prefetch" href="/assets/js/101.3e18b4d6.js"><link rel="prefetch" href="/assets/js/102.c7d7ed9c.js"><link rel="prefetch" href="/assets/js/103.4c771ce7.js"><link rel="prefetch" href="/assets/js/104.a2acd367.js"><link rel="prefetch" href="/assets/js/105.63399350.js"><link rel="prefetch" href="/assets/js/106.8a4744cc.js"><link rel="prefetch" href="/assets/js/107.7739d796.js"><link rel="prefetch" href="/assets/js/108.8008ae33.js"><link rel="prefetch" href="/assets/js/109.01c63898.js"><link rel="prefetch" href="/assets/js/11.7b837560.js"><link rel="prefetch" href="/assets/js/110.ad01aca9.js"><link rel="prefetch" href="/assets/js/111.3d74db70.js"><link rel="prefetch" href="/assets/js/112.67f55c34.js"><link rel="prefetch" href="/assets/js/113.016cd3c0.js"><link rel="prefetch" href="/assets/js/114.860e4b2b.js"><link rel="prefetch" href="/assets/js/115.b2f26258.js"><link rel="prefetch" href="/assets/js/116.8bea67b4.js"><link rel="prefetch" href="/assets/js/117.20682843.js"><link rel="prefetch" href="/assets/js/118.86a22e16.js"><link rel="prefetch" href="/assets/js/119.ad518bea.js"><link rel="prefetch" href="/assets/js/12.8fd78714.js"><link rel="prefetch" href="/assets/js/120.f97dd86f.js"><link rel="prefetch" href="/assets/js/121.c5601b4a.js"><link rel="prefetch" href="/assets/js/122.2889645a.js"><link rel="prefetch" href="/assets/js/13.ebf26820.js"><link rel="prefetch" href="/assets/js/14.b92dca09.js"><link rel="prefetch" href="/assets/js/15.b3dcef45.js"><link rel="prefetch" href="/assets/js/16.50378990.js"><link rel="prefetch" href="/assets/js/17.056bba2b.js"><link rel="prefetch" href="/assets/js/18.9047bb38.js"><link rel="prefetch" href="/assets/js/19.ab53b0fe.js"><link rel="prefetch" href="/assets/js/20.fe0b8af0.js"><link rel="prefetch" href="/assets/js/21.20a702f3.js"><link rel="prefetch" href="/assets/js/22.0ee08d29.js"><link rel="prefetch" href="/assets/js/23.02039a9a.js"><link rel="prefetch" href="/assets/js/24.363d3d74.js"><link rel="prefetch" href="/assets/js/25.dd6b101e.js"><link rel="prefetch" href="/assets/js/26.20493714.js"><link rel="prefetch" href="/assets/js/27.708723e5.js"><link rel="prefetch" href="/assets/js/28.4561834a.js"><link rel="prefetch" href="/assets/js/29.15cfcf5f.js"><link rel="prefetch" href="/assets/js/3.8d39d40c.js"><link rel="prefetch" href="/assets/js/30.04ab69de.js"><link rel="prefetch" href="/assets/js/31.b11c4302.js"><link rel="prefetch" href="/assets/js/32.81ae74ee.js"><link rel="prefetch" href="/assets/js/33.ddf09cc0.js"><link rel="prefetch" href="/assets/js/34.bc3549f7.js"><link rel="prefetch" href="/assets/js/35.4a0e0ebd.js"><link rel="prefetch" href="/assets/js/36.961a1c50.js"><link rel="prefetch" href="/assets/js/37.d8658de5.js"><link rel="prefetch" href="/assets/js/38.f76063aa.js"><link rel="prefetch" href="/assets/js/39.73edd08f.js"><link rel="prefetch" href="/assets/js/4.916af9bf.js"><link rel="prefetch" href="/assets/js/40.72314bff.js"><link rel="prefetch" href="/assets/js/41.c5eb1147.js"><link rel="prefetch" href="/assets/js/42.07aeafbb.js"><link rel="prefetch" href="/assets/js/43.c4a652ba.js"><link rel="prefetch" href="/assets/js/44.91fbe22b.js"><link rel="prefetch" href="/assets/js/45.6eecf0c4.js"><link rel="prefetch" href="/assets/js/46.81b7f108.js"><link rel="prefetch" href="/assets/js/47.b4468ab3.js"><link rel="prefetch" href="/assets/js/48.df205f23.js"><link rel="prefetch" href="/assets/js/49.7df59fa3.js"><link rel="prefetch" href="/assets/js/5.4fcd540a.js"><link rel="prefetch" href="/assets/js/50.0dc2fc5b.js"><link rel="prefetch" href="/assets/js/51.1ffabe54.js"><link rel="prefetch" href="/assets/js/52.2269d284.js"><link rel="prefetch" href="/assets/js/53.81b23843.js"><link rel="prefetch" href="/assets/js/54.b3249def.js"><link rel="prefetch" href="/assets/js/55.50dcf2af.js"><link rel="prefetch" href="/assets/js/56.c399c272.js"><link rel="prefetch" href="/assets/js/57.024f1dbb.js"><link rel="prefetch" href="/assets/js/58.680c6cf4.js"><link rel="prefetch" href="/assets/js/59.2291e9d7.js"><link rel="prefetch" href="/assets/js/6.7610d1d4.js"><link rel="prefetch" href="/assets/js/60.e59a26c1.js"><link rel="prefetch" href="/assets/js/61.ff5c64e4.js"><link rel="prefetch" href="/assets/js/62.7d97062c.js"><link rel="prefetch" href="/assets/js/63.6456dc7e.js"><link rel="prefetch" href="/assets/js/64.0b055680.js"><link rel="prefetch" href="/assets/js/65.9f25601c.js"><link rel="prefetch" href="/assets/js/67.9b8f9716.js"><link rel="prefetch" href="/assets/js/68.d6c1869a.js"><link rel="prefetch" href="/assets/js/69.0a3688dd.js"><link rel="prefetch" href="/assets/js/7.9ebbd956.js"><link rel="prefetch" href="/assets/js/70.22f48891.js"><link rel="prefetch" href="/assets/js/71.9eb4fda9.js"><link rel="prefetch" href="/assets/js/72.ca4fa80c.js"><link rel="prefetch" href="/assets/js/73.17d1ab37.js"><link rel="prefetch" href="/assets/js/74.139691db.js"><link rel="prefetch" href="/assets/js/75.08d0560f.js"><link rel="prefetch" href="/assets/js/76.61078c10.js"><link rel="prefetch" href="/assets/js/77.4482adcc.js"><link rel="prefetch" href="/assets/js/78.166f2c37.js"><link rel="prefetch" href="/assets/js/79.2c7d6b79.js"><link rel="prefetch" href="/assets/js/8.6f8916c2.js"><link rel="prefetch" href="/assets/js/80.80445c3f.js"><link rel="prefetch" href="/assets/js/81.14e3bae8.js"><link rel="prefetch" href="/assets/js/82.7dac687f.js"><link rel="prefetch" href="/assets/js/83.1dd2e78e.js"><link rel="prefetch" href="/assets/js/84.23b759de.js"><link rel="prefetch" href="/assets/js/85.7574f69f.js"><link rel="prefetch" href="/assets/js/86.102dac61.js"><link rel="prefetch" href="/assets/js/87.8f5d639e.js"><link rel="prefetch" href="/assets/js/88.e564cbbe.js"><link rel="prefetch" href="/assets/js/89.cd66eb78.js"><link rel="prefetch" href="/assets/js/9.baa162db.js"><link rel="prefetch" href="/assets/js/90.685c6878.js"><link rel="prefetch" href="/assets/js/91.b8d1b7ea.js"><link rel="prefetch" href="/assets/js/92.1faa374d.js"><link rel="prefetch" href="/assets/js/93.cf5ff33c.js"><link rel="prefetch" href="/assets/js/94.8a02aefb.js"><link rel="prefetch" href="/assets/js/95.1b379a5b.js"><link rel="prefetch" href="/assets/js/96.9fd67185.js"><link rel="prefetch" href="/assets/js/97.c5a6a5a1.js"><link rel="prefetch" href="/assets/js/98.9a231411.js"><link rel="prefetch" href="/assets/js/99.5b344cab.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9fff9873.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">anyang的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/go/introduction.html" class="sidebar-link">Go语言</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Go教程</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go/learn-go/10 从零搭建Go语言开发环境.html" class="sidebar-link">从零搭建Go语言开发环境</a></li><li><a href="/go/learn-go/11 第一个Go程序.html" class="sidebar-link">第一个Go程序Hello World</a></li><li><a href="/go/learn-go/12 Go 语言数据类型.html" class="active sidebar-link">Go语言数据类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/learn-go/12 Go 语言数据类型.html#整型" class="sidebar-link">整型</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/12 Go 语言数据类型.html#浮点型" class="sidebar-link">浮点型</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/12 Go 语言数据类型.html#布尔值" class="sidebar-link">布尔值</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/12 Go 语言数据类型.html#字符串" class="sidebar-link">字符串</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/12 Go 语言数据类型.html#字符串转义符" class="sidebar-link">字符串转义符</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/12 Go 语言数据类型.html#多行字符串" class="sidebar-link">多行字符串</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/12 Go 语言数据类型.html#字符串的常用操作" class="sidebar-link">字符串的常用操作</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/12 Go 语言数据类型.html#byte和rune类型" class="sidebar-link">byte和rune类型</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/12 Go 语言数据类型.html#修改字符串" class="sidebar-link">修改字符串</a></li><li class="sidebar-sub-header"><a href="/go/learn-go/12 Go 语言数据类型.html#类型转换" class="sidebar-link">类型转换</a></li></ul></li><li><a href="/go/learn-go/13 Go语言变量.html" class="sidebar-link">Go语言变量</a></li><li><a href="/go/learn-go/14 Go语言常量.html" class="sidebar-link">Go语言常量</a></li><li><a href="/go/learn-go/15 Go 语言运算符.html" class="sidebar-link">Go语言运算符</a></li><li><a href="/go/learn-go/16 Go语言条件语句.html" class="sidebar-link">Go语言条件语句</a></li><li><a href="/go/learn-go/17 Go语言的循环语句.html" class="sidebar-link">Go语言的循环语句</a></li><li><a href="/go/learn-go/18 Go 语言数组.html" class="sidebar-link">Go语言数组</a></li><li><a href="/go/learn-go/19 Go语言切片.html" class="sidebar-link">Go语言基础之切片</a></li><li><a href="/go/learn-go/20 Go语言map.html" class="sidebar-link">Go语言基础之map</a></li><li><a href="/go/learn-go/21 Go语言指针.html" class="sidebar-link">Go语言指针</a></li><li><a href="/go/learn-go/22 Go语言函数.html" class="sidebar-link">Go语言函数</a></li><li><a href="/go/learn-go/23  fmt.Printf总结.html" class="sidebar-link">fmt.Printf总结</a></li><li><a href="/go/learn-go/24 Go语言常用方法总结.html" class="sidebar-link">Go常用方法总结</a></li><li><a href="/go/learn-go/25 Go语言结构体.html" class="sidebar-link">Go语言结构体</a></li><li><a href="/go/learn-go/26 Go语言方法.html" class="sidebar-link">Go语言方法</a></li><li><a href="/go/learn-go/27 Go语言接口.html" class="sidebar-link">Go语言接口</a></li><li><a href="/go/learn-go/28 Go语言并发编程.html" class="sidebar-link">Go语言并发编程</a></li><li><a href="/go/learn-go/29 Go语言文件操作.html" class="sidebar-link">Go语言文件操作</a></li><li><a href="/go/learn-go/30 Go语言单元测试.html" class="sidebar-link">Go语言单元测试</a></li><li><a href="/go/learn-go/31 Go语言之http.html" class="sidebar-link">Go语言之http</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go Web框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go工具中间件</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="go语言数据类型"><a href="#go语言数据类型" class="header-anchor">#</a> Go语言数据类型</h1> <p><a data-fancybox="" title="go语言数据类型" href="/img/goImage/go语言数据类型.png"><img src="/img/goImage/go%E8%AF%AD%E8%A8%80%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B.png" alt="go语言数据类型"></a></p> <h3 id="go-语言按类别有以下几种数据类型："><a href="#go-语言按类别有以下几种数据类型：" class="header-anchor">#</a> Go 语言按类别有以下几种数据类型：</h3> <table><thead><tr><th style="text-align:left;">数据类型</th> <th>描述</th></tr></thead> <tbody><tr><td style="text-align:left;">布尔型</td> <td>布尔型的值只可以是常量 true 或者 false。一个简单的例子：var b bool = true。</td></tr> <tr><td style="text-align:left;">数字类型</td> <td>整型 int 和浮点型 float32、float64，Go 语言支持整型和浮点型数字，并且原生支持复数，其中位的运算采用补码。</td></tr> <tr><td style="text-align:left;">字符串类型</td> <td>字符串就是一串固定长度的字符连接起来的字符序列。Go的字符串是由单个字节连接起来的。Go语言的字符串的字节使用UTF-8编码标识Unicode文本。</td></tr> <tr><td style="text-align:left;">派生类型</td> <td>(a) 指针类型（Pointer）  (b) 数组类型  (c) 结构化类型(struct)  (d) Channel 类型  (e) 函数类型  (f) 切片类型  (g) 接口类型（interface）  (h) Map 类型</td></tr></tbody></table> <h4 id="go在大小写方面它有一些默认的行为："><a href="#go在大小写方面它有一些默认的行为：" class="header-anchor">#</a> Go在大小写方面它有一些默认的行为：</h4> <ul><li>大写字母开头的变量是，是公有变量；小写字母开头的是私有变量。</li> <li>大写字母开头的函数也是一样，相当于java中的带public关键词的公有函数；</li> <li>小写字母开头的相当于private关键词的私有函数。</li></ul> <h3 id="数字类型："><a href="#数字类型：" class="header-anchor">#</a> 数字类型：</h3> <p>Go 也有基于架构的类型，例如：int、uint 和 uintptr。</p> <table><thead><tr><th>序号</th> <th>类型和描述</th></tr></thead> <tbody><tr><td>1</td> <td><strong>uint8</strong>无符号 8 位整型 (0 到 255)</td></tr> <tr><td>2</td> <td><strong>uint16</strong>无符号 16 位整型 (0 到 65535)</td></tr> <tr><td>3</td> <td><strong>uint32</strong>无符号 32 位整型 (0 到 4294967295)</td></tr> <tr><td>4</td> <td><strong>uint64</strong>无符号 64 位整型 (0 到 18446744073709551615)</td></tr> <tr><td>5</td> <td><strong>int8</strong>有符号 8 位整型 (-128 到 127)</td></tr> <tr><td>6</td> <td><strong>int16</strong>有符号 16 位整型 (-32768 到 32767)</td></tr> <tr><td>7</td> <td><strong>int32</strong>有符号 32 位整型 (-2147483648 到 2147483647)</td></tr> <tr><td>8</td> <td><strong>int64</strong>有符号 64 位整型 (-9223372036854775808 到 9223372036854775807)</td></tr></tbody></table> <h3 id="浮点型："><a href="#浮点型：" class="header-anchor">#</a> 浮点型：</h3> <table><thead><tr><th>序号</th> <th>类型和描述</th></tr></thead> <tbody><tr><td>1</td> <td><strong>float32</strong>IEEE-754 32位浮点型数</td></tr> <tr><td>2</td> <td><strong>float64</strong>IEEE-754 64位浮点型数</td></tr> <tr><td>3</td> <td><strong>complex64</strong>32 位实数和虚数</td></tr> <tr><td>4</td> <td><strong>complex128</strong>64 位实数和虚数</td></tr></tbody></table> <h3 id="其他数字类型"><a href="#其他数字类型" class="header-anchor">#</a> 其他数字类型</h3> <table><thead><tr><th>序号</th> <th>类型和描述</th></tr></thead> <tbody><tr><td>1</td> <td><strong>byte</strong>类似 uint8</td></tr> <tr><td>2</td> <td><strong>rune</strong>类似 int32</td></tr> <tr><td>3</td> <td><strong>uint</strong>32 或 64 位</td></tr> <tr><td>4</td> <td><strong>int</strong>与 uint 一样大小</td></tr> <tr><td>5</td> <td><strong>uintptr</strong>无符号整型，用于存放一个指针</td></tr></tbody></table> <h3 id="复合类型-派生类型"><a href="#复合类型-派生类型" class="header-anchor">#</a> 复合类型(派生类型)</h3> <table><thead><tr><th>序号</th> <th>类型和描述</th></tr></thead> <tbody><tr><td>1</td> <td>指针类型（Pointer）</td></tr> <tr><td>2</td> <td>数组类型</td></tr> <tr><td>3</td> <td>结构化类型(struct)</td></tr> <tr><td>4</td> <td>Channel 类型</td></tr> <tr><td>5</td> <td>函数类型</td></tr> <tr><td>6</td> <td>切片类型</td></tr> <tr><td>7</td> <td>接口类型（interface）</td></tr> <tr><td>8</td> <td>Map 类型</td></tr></tbody></table> <h3 id="举例如下"><a href="#举例如下" class="header-anchor">#</a> 举例如下</h3> <h2 id="整型"><a href="#整型" class="header-anchor">#</a> 整型</h2> <p>Go1.13版本之后引入了数字字面量语法，这样便于开发者以二进制、八进制或十六进制浮点数的格式定义数字，例如：
v := 0b00101101， 代表二进制的 101101，相当于十进制的 45。 v := 0o377，代表八进制的 377，相当于十进制的 255。 v := 0x1p-2，代表十六进制的 1 除以 2²，也就是 0.25。 而且还允许我们用 _ 来分隔数字，比如说：
v := 123_456 等于 123456。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main
 
<span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>
 
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token comment">// 十进制</span>
	<span class="token keyword">var</span> a <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">10</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d \n&quot;</span><span class="token punctuation">,</span> a<span class="token punctuation">)</span>  <span class="token comment">// 10</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%b \n&quot;</span><span class="token punctuation">,</span> a<span class="token punctuation">)</span>  <span class="token comment">// 1010  占位符%b表示二进制</span>
 
	<span class="token comment">// 八进制  以0开头</span>
	<span class="token keyword">var</span> b <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">077</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%o \n&quot;</span><span class="token punctuation">,</span> b<span class="token punctuation">)</span>  <span class="token comment">// 77</span>
 
	<span class="token comment">// 十六进制  以0x开头</span>
	<span class="token keyword">var</span> c <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">0xff</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%x \n&quot;</span><span class="token punctuation">,</span> c<span class="token punctuation">)</span>  <span class="token comment">// ff</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%X \n&quot;</span><span class="token punctuation">,</span> c<span class="token punctuation">)</span>  <span class="token comment">// FF</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h2 id="浮点型"><a href="#浮点型" class="header-anchor">#</a> 浮点型</h2> <p>Go语言支持两种浮点型数：float32和float64。这两种浮点型数据格式遵循IEEE 754标准： float32 的浮点数的最大范围约为 3.4e38，可以使用常量定义：math.MaxFloat32。 float64 的浮点数的最大范围约为 1.8e308，可以使用一个常量定义：math.MaxFloat64。
打印浮点数时，可以使用fmt包配合动词%f，代码如下</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main
<span class="token keyword">import</span> <span class="token punctuation">(</span>
        <span class="token string">&quot;fmt&quot;</span>
        <span class="token string">&quot;math&quot;</span>
<span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%f\n&quot;</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span>Pi<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%.2f\n&quot;</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span>Pi<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="布尔值"><a href="#布尔值" class="header-anchor">#</a> 布尔值</h2> <p>Go语言中以bool类型进行声明布尔型数据，布尔型数据只有true（真）和false（假）两个值</p> <p>注意：</p> <ul><li>布尔类型变量的默认值为false。</li> <li>Go 语言中不允许将整型强制转换为布尔型.</li> <li>布尔型无法参与数值运算，也无法与其他类型进行转换。</li></ul> <h2 id="字符串"><a href="#字符串" class="header-anchor">#</a> 字符串</h2> <p>Go语言中的字符串以原生数据类型出现，使用字符串就像使用其他原生数据类型（int、bool、float32、float64 等）一样。 Go 语言里的字符串的内部实现使用UTF-8编码。 字符串的值为双引号(&quot;)中的内容，可以在Go语言的源码中直接添加非ASCII码字符，例如：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>s1 <span class="token operator">:=</span> <span class="token string">&quot;hello&quot;</span>
s2 <span class="token operator">:=</span> <span class="token string">&quot;你好&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="字符串转义符"><a href="#字符串转义符" class="header-anchor">#</a> 字符串转义符</h2> <p>Go 语言的字符串常见转义符包含回车、换行、单双引号、制表符等，如下表所示。</p> <table><thead><tr><th>转义符</th> <th>转义符</th></tr></thead> <tbody><tr><td>\r</td> <td>回车符（返回行首）</td></tr> <tr><td>\n</td> <td>换行符（直接跳到下一行的同列位置）</td></tr> <tr><td>\t</td> <td>制表符</td></tr> <tr><td>'</td> <td>单引号</td></tr> <tr><td>&quot;</td> <td>双引号</td></tr> <tr><td>\</td> <td>反斜杠</td></tr></tbody></table> <h2 id="多行字符串"><a href="#多行字符串" class="header-anchor">#</a> 多行字符串</h2> <p>Go语言中要定义一个多行字符串时，就必须使用反引号`字符：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>s1 <span class="token operator">:=</span> <span class="token string">`第一行
第二行
第三行
`</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>反引号间换行将被作为字符串中的换行，但是所有的转义字符均无效，文本将会原样输出。</p> <h2 id="字符串的常用操作"><a href="#字符串的常用操作" class="header-anchor">#</a> 字符串的常用操作</h2> <table><thead><tr><th>方法</th> <th>方法</th></tr></thead> <tbody><tr><td>len(str)</td> <td>求长度</td></tr> <tr><td>+或fmt.Sprintf</td> <td>拼接字符串</td></tr> <tr><td>strings.Split</td> <td>分割</td></tr> <tr><td>strings.contains</td> <td>判断是否包含</td></tr> <tr><td>strings.HasPrefix,strings.HasSuffix</td> <td>前缀/后缀判断</td></tr> <tr><td>strings.Index(),strings.LastIndex()</td> <td>子串出现的位置</td></tr> <tr><td>strings.Join(a[]string, sep string)</td> <td>join操作</td></tr></tbody></table> <h2 id="byte和rune类型"><a href="#byte和rune类型" class="header-anchor">#</a> byte和rune类型</h2> <p>组成每个字符串的元素叫做“字符”，可以通过遍历或者单个获取字符串元素获得字符。 字符用单引号（’）包裹起来，如：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> a <span class="token operator">:=</span> <span class="token string">'中'</span>
<span class="token keyword">var</span> b <span class="token operator">:=</span> <span class="token string">'x'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>Go 语言的字符有以下两种：</p> <ul><li>uint8类型，或者叫 byte 型，代表了ASCII码的一个字符。</li> <li>rune类型，代表一个 UTF-8字符。
当需要处理中文、日文或者其他复合字符时，则需要用到rune类型。rune类型实际是一个int32。</li></ul> <p>Go 使用了特殊的 rune 类型来处理 Unicode，让基于 Unicode 的文本处理更为方便，也可以使用 byte 型进行默认字符串处理，性能和扩展性都有照顾。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 遍历字符串</span>
<span class="token keyword">func</span> <span class="token function">traversalString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	s <span class="token operator">:=</span> <span class="token string">&quot;hello沙河&quot;</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token function">len</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span> <span class="token comment">//byte</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%v(%c) &quot;</span><span class="token punctuation">,</span> s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> r <span class="token operator">:=</span> <span class="token keyword">range</span> s <span class="token punctuation">{</span> <span class="token comment">//rune</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%v(%c) &quot;</span><span class="token punctuation">,</span> r<span class="token punctuation">,</span> r<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>输出</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token function">104</span><span class="token punctuation">(</span>h<span class="token punctuation">)</span> <span class="token function">101</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token function">108</span><span class="token punctuation">(</span>l<span class="token punctuation">)</span> <span class="token function">108</span><span class="token punctuation">(</span>l<span class="token punctuation">)</span> <span class="token function">111</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token function">230</span><span class="token punctuation">(</span>æ<span class="token punctuation">)</span> <span class="token function">178</span><span class="token punctuation">(</span>²<span class="token punctuation">)</span> <span class="token function">153</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">230</span><span class="token punctuation">(</span>æ<span class="token punctuation">)</span> <span class="token function">178</span><span class="token punctuation">(</span>²<span class="token punctuation">)</span> <span class="token function">179</span><span class="token punctuation">(</span>³<span class="token punctuation">)</span> 
<span class="token function">104</span><span class="token punctuation">(</span>h<span class="token punctuation">)</span> <span class="token function">101</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token function">108</span><span class="token punctuation">(</span>l<span class="token punctuation">)</span> <span class="token function">108</span><span class="token punctuation">(</span>l<span class="token punctuation">)</span> <span class="token function">111</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token function">27801</span><span class="token punctuation">(</span>沙<span class="token punctuation">)</span> <span class="token function">27827</span><span class="token punctuation">(</span>河<span class="token punctuation">)</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>因为UTF8编码下一个中文汉字由3~4个字节组成，所以我们不能简单的按照字节去遍历一个包含中文的字符串，否则就会出现上面输出中第一行的结果。</p> <p>字符串底层是一个byte数组，所以可以和[]byte类型相互转换。字符串是不能修改的 字符串是由byte字节组成，所以字符串的长度是byte字节的长度。 rune类型用来表示utf8字符，一个rune字符由一个或多个byte组成。</p> <h2 id="修改字符串"><a href="#修改字符串" class="header-anchor">#</a> 修改字符串</h2> <p>要修改字符串，需要先将其转换成[]rune或[]byte，完成后再转换为string。无论哪种转换，都会重新分配内存，并复制字节数组。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">changeString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	s1 <span class="token operator">:=</span> <span class="token string">&quot;big&quot;</span>
	<span class="token comment">// 强制类型转换</span>
	byteS1 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span>
	byteS1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'p'</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>byteS1<span class="token punctuation">)</span><span class="token punctuation">)</span>

	s2 <span class="token operator">:=</span> <span class="token string">&quot;白萝卜&quot;</span>
	runeS2 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">rune</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span>
	runeS2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'红'</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>runeS2<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="类型转换"><a href="#类型转换" class="header-anchor">#</a> 类型转换</h2> <p>Go语言中只有强制类型转换，没有隐式类型转换。该语法只能在两个类型之间支持相互转换的时候使用。</p> <p>强制类型转换的基本语法如下：</p> <blockquote><p>T(表达式)
其中，T表示要转换的类型。表达式包括变量、复杂算子和函数返回值等.</p></blockquote> <p>比如计算直角三角形的斜边长时使用math包的Sqrt()函数，该函数接收的是float64类型的参数，而变量a和b都是int类型的，这个时候就需要将a和b强制类型转换为float64类型。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">sqrtDemo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">var</span> a<span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span>
	<span class="token keyword">var</span> c <span class="token builtin">int</span>
	<span class="token comment">// math.Sqrt()接收的参数是float64类型，需要强制转换</span>
	c <span class="token operator">=</span> <span class="token function">int</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Sqrt</span><span class="token punctuation">(</span><span class="token function">float64</span><span class="token punctuation">(</span>a<span class="token operator">*</span>a <span class="token operator">+</span> b<span class="token operator">*</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="练习编写代码统计出字符串-hello-中国你好-world-中汉字的数量。"><a href="#练习编写代码统计出字符串-hello-中国你好-world-中汉字的数量。" class="header-anchor">#</a> 练习编写代码统计出字符串&quot;Hello 中国你好 World&quot;中汉字的数量。</h4> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;unicode&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">isHan</span><span class="token punctuation">(</span>r <span class="token builtin">rune</span><span class="token punctuation">)</span> <span class="token builtin">bool</span> <span class="token punctuation">{</span>

	<span class="token keyword">return</span> unicode<span class="token punctuation">.</span><span class="token function">Is</span><span class="token punctuation">(</span>unicode<span class="token punctuation">.</span>Han<span class="token punctuation">,</span> r<span class="token punctuation">)</span>

<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	words<span class="token operator">:=</span><span class="token string">&quot;Hello 中国你好 World&quot;</span>
	<span class="token keyword">var</span> data <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">rune</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">rune</span><span class="token punctuation">(</span>words<span class="token punctuation">)</span>
	sum<span class="token operator">:=</span><span class="token number">0</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">len</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> i<span class="token operator">:=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token function">len</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span> <span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
		b <span class="token operator">:=</span> data<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
		<span class="token keyword">if</span> <span class="token function">isHan</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			sum<span class="token operator">++</span>
		<span class="token punctuation">}</span>

	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s含有%d个汉字&quot;</span><span class="token punctuation">,</span>words<span class="token punctuation">,</span>sum<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/go/learn-go/11 第一个Go程序.html" class="prev">
        第一个Go程序Hello World
      </a></span> <span class="next"><a href="/go/learn-go/13 Go语言变量.html">
        Go语言变量
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.1bc80adb.js" defer></script><script src="/assets/js/2.7c0608ab.js" defer></script><script src="/assets/js/66.cd973084.js" defer></script>
  </body>
</html>
